VERSION 0.6
ARG DOCKERHUB_USER_SECRET=+secrets/DOCKERHUB_USER
ARG DOCKERHUB_TOKEN_SECRET=+secrets/DOCKERHUB_TOKEN
ARG DOCKERHUB_MIRROR
ARG DOCKERHUB_MIRROR_INSECURE=false
ARG DOCKERHUB_AUTH=true
FROM ../..+earthly-integration-test-base \
    --DOCKERHUB_AUTH=$DOCKERHUB_AUTH \
    --DOCKERHUB_USER_SECRET=$DOCKERHUB_USER_SECRET \
    --DOCKERHUB_TOKEN_SECRET=$DOCKERHUB_TOKEN_SECRET \
    --DOCKERHUB_MIRROR=$DOCKERHUB_MIRROR \
    --DOCKERHUB_MIRROR_INSECURE=$DOCKERHUB_MIRROR_INSECURE

WORKDIR /test
ARG REGISTRY
ARG EARTHLY_BUILD_ARGS="REGISTRY"
ARG REGISTRY_CONFIG="
[registry.\"$REGISTRY\"]
  http = true
  insecure = true
"

COPY test.earth ./Earthfile

all:
    BUILD +test1
    BUILD +test2
    BUILD +test3

test1:
    RUN echo "content" >./input
    # Running with tmpfs mount = no local cache.
    DO +DO_REMOTE_CACHE_EARTHLY --target=+test1
    # Not cached.
    RUN cat ./output
    RUN nl=$(cat ./output | grep "execute-test1-run-before-copy" | wc -l) && \
        test "$nl" -eq 2
    # Not cached.
    RUN cat ./output
    RUN nl=$(cat ./output | grep "execute-test1-run-after-copy" | wc -l) && \
        test "$nl" -eq 2
    # No change & re-run.
    DO +DO_REMOTE_CACHE_EARTHLY --target=+test1
    # Cached.
    RUN nl=$(cat ./output | grep "execute-test1-run-before-copy" | wc -l) && \
        test "$nl" -eq 1
    # Cached.
    RUN nl=$(cat ./output | grep "execute-test1-run-after-copy" | wc -l) && \
        test "$nl" -eq 1
    # Change input & re-run.
    RUN echo "other content" >./input
    DO +DO_REMOTE_CACHE_EARTHLY --target=+test1
    # Cached.
    RUN nl=$(cat ./output | grep "execute-test1-run-before-copy" | wc -l) && \
        test "$nl" -eq 1
    # Not cached.
    RUN nl=$(cat ./output | grep "execute-test1-run-after-copy" | wc -l) && \
        test "$nl" -eq 2

test2:
    RUN echo "a"
    COPY --dir test2 test2
    # Running with tmpfs mount = no local cache.
    DO +DO_REMOTE_CACHE_EARTHLY --target=+test2
    DO +DO_REMOTE_CACHE_EARTHLY --target=+test2
    # TODO FIXME the remote-cache caching isn't always working; we will ignore these checks until the flakyness is fixed.
    RUN (cat ./output | grep '\*cached\* --> RUN npm install') || echo "WARNING: RUN command not cached"
    RUN (cat ./output | grep '\*cached\* --> COPY test2/dist dist') || echo "WARNING: COPY classical command not cached"
    RUN (cat ./output | grep '\*cached\* --> SAVE ARTIFACT dist +test2-build/dist') || echo "WARNING: SAVE ARTIFACT command not cached"
    RUN (cat ./output | grep '\*cached\* --> COPY +test2-build/dist ./dist') || echo "WARNING: COPY target command not cached"

test3:
    # Running with tmpfs mount = no local cache.
    RUN --privileged \
        --entrypoint \
        --mount=type=tmpfs,target=/tmp/earthly \
        -- --ci --push +test3

# Work around the lack of variable overriding, since the base image already includes EARTHLY_ADDITIONAL_BUILDKIT_CONFIG
DO_REMOTE_CACHE_EARTHLY:
    COMMAND

    ARG EARTHLY_ADDITIONAL_BUILDKIT_CONFIG
    ARG REGISTRY_CONFIG
    ARG target

    RUN --privileged \
        --mount=type=tmpfs,target=/tmp/earthly \
        -- \
        EARTHLY_ADDITIONAL_BUILDKIT_CONFIG=$EARTHLY_ADDITIONAL_BUILDKIT_CONFIG$REGISTRY_CONFIG \
        /usr/bin/earthly-entrypoint.sh --ci --push \
            --build-arg REGISTRY=$REGISTRY \
            --build-arg DOCKERHUB_AUTH=$DOCKERHUB_AUTH \
            --build-arg DOCKERHUB_USER_SECRET=$DOCKERHUB_USER_SECRET \
            --build-arg DOCKERHUB_TOKEN_SECRET=$DOCKERHUB_TOKEN_SECRET \
            --build-arg DOCKERHUB_MIRROR=$DOCKERHUB_MIRROR \
            --build-arg DOCKERHUB_MIRROR_INSECURE=$DOCKERHUB_MIRROR_INSECURE \
            $target 2>&1 | tee ./output
